package by.alex.dzeshko.lastfmsimple.content.provider;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import by.alex.dzeshko.lastfmsimple.bo.TrackModel;
import by.alex.dzeshko.lastfmsimple.content.Contract;
import by.alex.dzeshko.lastfmsimple.content.provider.common.CommonProvider;
import by.alex.dzeshko.lastfmsimple.utils.Md5;

public class TracksProvider extends CommonProvider {

	private static final String LOG_TAG = "TracksProvider";

	@Override
	protected String getOrderBy() {
		return null;
	}

	@Override
	protected String getTableName() {
		return Contract.TracksColumns.TABLE_NAME;
	}

	@Override
	protected String getContentType() {
		return Contract.TracksColumns.CONTENT_TYPE;
	}

	@Override
	protected Uri getContentURI() {
		return Contract.TracksColumns.CONTENT_URI;
	}

	@Override
	protected String[] getColumns() {
		return Contract.TracksColumns.COLUMNS;
	}

	@Override
	protected ContentValues getContentValues(JSONObject jsonObject)
			throws JSONException {
		ContentValues values = new ContentValues();
		TrackModel track = new TrackModel(jsonObject);
		String mbid = track.getMbid();
		String name = track.getName();
		if(TextUtils.isEmpty(mbid)){
			Log.d(LOG_TAG, name);
			mbid = Md5.convert(name);
			Log.d(LOG_TAG, mbid);
		}
		values.put(Contract.TracksColumns.MBID, mbid);
		values.put(Contract.TracksColumns.NAME, name);
		values.put(Contract.TracksColumns.ARTIST, track.getArtist());
		values.put(Contract.TracksColumns.SUMMARY, track.getSummary());
		values.put(Contract.TracksColumns.DURATION, track.getDuration());
		values.put(Contract.TracksColumns.IMAGE_SMALL, track.getImage(0));
		values.put(Contract.TracksColumns.IMAGE_MEDIUM, track.getImage(1));
		values.put(Contract.TracksColumns.IMAGE_LARGE, track.getImage(2));
		values.put(Contract.TracksColumns.IMAGE_XLARGE, track.getImage(3));
		values.put(Contract.TracksColumns.MARKER, track.getMarker());
		return values;
	}

}
